package self.learn.record.config;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import self.learn.record.model.User;

/**
 * Created by Mercy Yang on 2017/10/13.
 */
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    @Override
    protected Class<?> getBeanClass(Element element) {
        return User.class;
    }

    @Override
    protected void doParse(Element element,  BeanDefinitionBuilder builder) {
        String firstName = element.getAttribute("first-name");
        String lastName = element.getAttribute("last-name");
        int age = Integer.valueOf(element.getAttribute("age"));

        if (StringUtils.hasText(firstName)){
            builder.addPropertyValue("firstName",firstName);
        }
        if(StringUtils.hasText(lastName)){
            builder.addPropertyValue("lastName",lastName);
        }
        builder.addPropertyValue("age",age);
    }
}
